package com.me.chat.aop;

import com.me.chat.pojo.suport.LogRecord;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 全局的 切面日志统计  日志太碍事了，目前先关了
 * @Author: qiu
 * @Date: 2021/2/2 15:31
 */

@Aspect
@Slf4j
//@Component
public class GlobalAop {

    /**
     * 专走支撑  进出监控
     * 定义切入点，切入点为com.zp.aokires.controller.suport下的所有接口
     */
    @Pointcut("execution(public * com.me.chat.controller.suport.*.*(..))")
    public void webLog(){}

    /**
     * 前置通知：在连接点之前执行的通知
     * @param joinPoint
     * @throws Throwable
     */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //日志数据装载
        LogRecord logRecord = new LogRecord(
                request.getRequestURL().toString(),//url
                request.getMethod(),//http_method
                request.getRemoteAddr(),//ip
                joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName(),//class_method
                Arrays.toString(joinPoint.getArgs())//params
        );
        // 记录下请求内容
        log.info("安全监控 : " + logRecord.toString());
    }
}
